import java.util.ArrayList;
import java.util.List;

/**
 * Given a sorted integer array without duplicates, 
 * return the summary of its ranges.
 * For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].
 * @author Jingjing Wang
 *2015-6-28
 */
public class Solution {
	public static void main(String[] args) {
		int[] array = { 0, 1, 3, 4 };
		Solution solution = new Solution();
		System.out.println(solution.summaryRanges(array));
	}

	public List<String> summaryRanges(int[] nums) {
		int start = nums[0];
		List<String> result = new ArrayList<String>();
		for (int i = 1; i < nums.length; i++) {
			if (nums[i] != nums[i - 1] + 1) {
				// end an old range and start a new range
				if (nums[i - 1] == start) {
					result.add(start + "");
				} else {
					result.add(start + "->" + nums[i - 1]);
				}
				start = nums[i];
			}
		}
		if (nums[nums.length - 1] == start) {
			result.add(start + "");
		} else {
			result.add(start + "->" + nums[nums.length - 1]);
		}
		return result;
	}
}
